package ${package.Controller};

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.validation.Valid;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import com.jp.common.core.domain.R;

<#if restControllerStyle?? && restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

/**
* <p>
    * ${table.comment!} 前端控制器
    * </p>
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle?? && restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle?? && controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(tags = "${table.comment!}控制器")
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} ${table.serviceName?uncap_first};

    /**
     * 查询${table.comment!}列表
     *
     * @param ${entity?uncap_first} ${table.comment!}对象
     * @return ${table.comment!}集合
     */
    @GetMapping("/list")
    @ApiOperation("查询${table.comment!}列表")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:list')")
    public R<List<${entity}>> list(${entity} ${entity?uncap_first}) {
        List<${entity}> list = ${table.serviceName?uncap_first}.list();
        return R.ok(list);
    }

    /**
     * 获取${table.comment!}详细信息
     *
     * @param id ${table.comment!}ID
     * @return ${table.comment!}详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("获取${table.comment!}详细信息")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:query')")
    public R<${entity}> getInfo(@PathVariable Long id) {
        ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.getById(id);
        return R.ok(${entity?uncap_first});
    }

    /**
     * 新增${table.comment!}
     *
     * @param ${entity?uncap_first} ${table.comment!}对象
     * @return 结果
     */
    @PostMapping
    @ApiOperation("新增${table.comment!}")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:add')")
    public R<Boolean> add(@RequestBody @Valid ${entity} ${entity?uncap_first}) {
        boolean result = ${table.serviceName?uncap_first}.save(${entity?uncap_first});
        return R.ok(result);
    }

    /**
     * 修改${table.comment!}
     *
     * @param ${entity?uncap_first} ${table.comment!}对象
     * @return 结果
     */
    @PutMapping
    @ApiOperation("修改${table.comment!}")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:edit')")
    public R<Boolean> edit(@RequestBody @Valid ${entity} ${entity?uncap_first}) {
        boolean result = ${table.serviceName?uncap_first}.updateById(${entity?uncap_first});
        return R.ok(result);
    }

    /**
     * 删除${table.comment!}
     *
     * @param ids 需要删除的${table.comment!}ID数组
     * @return 结果
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除${table.comment!}")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:remove')")
    public R<Boolean> remove(@PathVariable Long[] ids) {
        boolean result = ${table.serviceName?uncap_first}.removeByIds(java.util.Arrays.asList(ids));
        return R.ok(result);
    }

    /**
     * 导出${table.comment!}数据为Excel
     *
     * @param response 响应对象
     * @throws IOException IO异常
     */
    @GetMapping("/export")
    @ApiOperation("导出${table.comment!}数据")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:export')")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<${entity}> list = ${table.serviceName?uncap_first}.list();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        String fileName = URLEncoder.encode("${table.comment!}数据.xlsx", StandardCharsets.UTF_8.name());
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), ${entity}.class).sheet("${table.comment!}").doWrite(list);
    }

    /**
     * 从Excel导入${table.comment!}数据
     *
     * @param file Excel文件
     * @return 导入结果
     */
    @PostMapping("/import")
    @ApiOperation("导入${table.comment!}数据")
    @PreAuthorize("@ss.hasPermi('${(permissionPrefix)!table.entityPath}:import')")
    public R<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<${entity}> list = EasyExcel.read(file.getInputStream())
                .head(${entity}.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet()
                .doReadSync();
        boolean result = ${table.serviceName?uncap_first}.saveBatch(list);
        return R.ok(result);
    }
}